package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.DeviceWarningDao;
import cn.topevery.garden.entity.po.DeviceWarning;
import cn.topevery.garden.entity.query.DeviceWarningQuery;
import cn.topevery.garden.entity.vo.DeviceWarningVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 设备报警记录表
 *
 * @author code-generate
 * @date 2021-09-06
 */
@Repository
public class DeviceWarningDaoImpl extends BaseDaoImpl<DeviceWarning> implements DeviceWarningDao {
    @Override
    public <P extends DbCsp> List<DeviceWarning> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<DeviceWarningVO> listPage(DeviceWarningQuery query) {
        String table = "t_device_warning tdw left join t_event_info tei on tdw.c_event_code = tei.c_code";

        String columns = "tdw.*,tei.c_address as address";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tdw.c_db_create_date desc");
        sqlQueryMate
                .where(true, "tdw.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getWarningStatus() != null, "tdw.c_warning_status", ScriptConditionEnum.Eq, query.getWarningStatus())
                .where(StringUtils.isNotBlank(query.getCode()), "tdw.c_device_code", ScriptConditionEnum.Like, query.getCode());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, DeviceWarningVO.class);
    }

    @Override
    public void changeWarningStatus(String eventCode) {
        String table = "t_device_warning tdw";

        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate
                .setColumnValue(true, "tdw.c_warning_status", 1)
                .where(true, "tdw.c_event_code", ScriptConditionEnum.Eq, eventCode);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    /**
     * 描述 公园一张图-运行监测-今日报警列表
     *
     * @return the today list data
     * @author wenzhao.lin
     * @created 2021 -09-14 17:55:06
     */
    @Override
    public List<DeviceWarningVO> getTodayListData() {
        StringBuffer sb = new StringBuffer();
        sb.append("select d.*,o.c_object_id from t_device_warning d " +
                "left join t_device_object_relate o on o.c_code = d.c_device_code " +
                "where d.c_db_status=0 and date(d.c_db_create_date) = date(now()) order by d.c_db_create_date desc ");

        AtomicReference<List<DeviceWarningVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(DeviceWarningVO.class));
        });
        return vo.get();
    }

    @Override
    public List<Map<String, Object>> warningCount(Integer type) {
        String date = "";
        Calendar now = Calendar.getInstance();
        Integer year = now.get(Calendar.YEAR);
        Integer month = now.get(Calendar.MONTH) + 1;
        Integer day = now.get(Calendar.DAY_OF_MONTH);
        if (type == 1) {
            date = year +"-01-01 00:00:00";
        } else if (type == 2) {
            date = year + "-"+month+"-01 00:00:00";
        } else {
            date = year + "-" + month + "-" + day + " 00:00:00";
        }
        String table = "t_sys_class tsc left join (select t.c_warning_type_id,t.c_warning_type_name warningTypeName,count(1) value from t_device_warning t where t.c_db_create_date >= '"
                + date +"' group by t.c_warning_type_id,t.c_warning_type_name) tc on tsc.c_class_id = tc.c_warning_type_id";
        SqlQueryMate sql = new SqlQueryMate(table).setColumns("tsc.c_name as warningTypeName,IFNULL(tc.value, 0) value");

        sql.where(true, "tsc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tsc.c_class_pid", ScriptConditionEnum.Eq, "D45DC8A7-C01F-4371-BC61-5E5229998734");
//        sql.setGroupBy("t.c_type,t.c_type_name,t.c_warning_type_id,t.c_warning_type_name");

        return getMap(sql);
    }
}
